/**
 * MoteAppKeyboardActivity.java
 *
 * Created by Andrew Moss on 18 May 2011

 */
package com.mossdev.android.moteapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.mossdev.android.moteapp.comms.Commands;
import com.mossdev.android.moteapp.comms.ServerConnectionException;

/**
 * An activity for sending keyboard commands to the mote server.
 * @author Andrew Moss
 */
public class MoteAppKeyboardActivity extends Activity{
	
	//Listeners
	//---------------------------------------
	
	//Send keyboard command button
	private OnClickListener sendcmd_clicklisten = new OnClickListener() {
		public void onClick(View v) {
			//Get cmd from user
			String keystring = ((TextView)findViewById(R.id.KeyboardText)).getText().toString();
			if (keystring.length() < 0) {
				Log.w(Config.LogTag, "User tried to send a zero length keyboard string.");
				return;
			}
			try {
				MoteApp.servercomms.sendCommand(Commands.PREFIX_KEY + keystring);
			} catch (ServerConnectionException e) {
				Log.e(Config.LogTag, e.err_msg);
			}
		}
	};
	
	//Methods
	//---------------------------------------
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.keyboard);

		// Set up Button
		Button button = (Button) findViewById(R.id.SendKeyboardCmd);
		button.setOnClickListener(sendcmd_clicklisten);
	}
}
